package com.jprocms.module.cms.controller.admin.vote.vo;

import cn.hutool.core.collection.CollUtil;
import com.jprocms.module.cms.controller.admin.voteitem.vo.VoteItemRespVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.jprocms.module.cms.constant.VoteConstants.*;

@Schema(description = "管理后台 - 投票 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class VoteRespVO extends VoteBaseVO {

    /**
     * 总票数
     */
    @Schema(description = "总投票数", example = "16241")
    public int getTotal() {
        List<VoteItemRespVO> itemRespVOS = getItems();
        if (CollUtil.isEmpty(itemRespVOS)) {
            return 0;
        }
        int sum = itemRespVOS.stream().mapToInt(VoteItemRespVO::getCount).sum();
        return sum;
    }

    /**
     * 状态
     */
    @Schema(description = "状态 0正常 1未启用  2 未开始 3已结束", example = "1")
    public short getStatus() {
        Date now = Calendar.getInstance().getTime();
        if (Boolean.FALSE.equals(getEnabled())) {
            return STATUS_DISABLED;
        } else if (getBeginDate() != null && now.compareTo(getBeginDate()) < 0) {
            return STATUS_NOT_STARTED;
        } else if (getEndDate() != null && now.compareTo(getEndDate()) > 0) {
            return STATUS_HAS_ENDED;
        } else {
            return STATUS_NORMAL;
        }
    }

    @Schema(description = "ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "8717")
    private Long id;

    @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime createTime;

    @Schema(description = "投票选项")
    private List<VoteItemRespVO> items = new ArrayList<>();

}
